﻿using CloudStorage_Backend.dao;
using CloudStorage_Backend.exception;
using CloudStorage_Backend.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CloudStorage_Backend.service
{
    public class UserService
    {
        private readonly UserDao userDao;

        public UserService(UserDao userDao)
        {
            this.userDao = userDao;
        }

        public User Login(User user)
        {
        
            User userFromDb = userDao.FindUserByName(user.UserName);
            if(userFromDb == null)
            {
                throw new UserException(UserExceptionMsg.USER_NOT_FOUND);
            }
            if(userFromDb.Password != user.Password)
            {
                throw new UserException(UserExceptionMsg.NAME_OR_PASSWORD_WRONG);
            }
            return userFromDb;
        }

        public User Register(User user)
        {
            User userFromDb = userDao.FindUserByName(user.UserName);
            if(userFromDb != null)
            {
                throw new UserException(UserExceptionMsg.NAME_HAS_BEEN_USED);
            }
            return userDao.AddUser(user);
        }
    }
}
